package fun.yyds.common.util;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.collect.Sets;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.BeanUtils;

import java.util.*;


public class PojoUtil {

    public static final String AUTH_EU_DATA_SWITCH = "AUTH_EU_DATA_SWITCH";


    public static <T> T getPojo(Object o, Class<T> tClass) {
        T o1 = null;
        try {
            o1 = tClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(o, o1);
        return o1;
    }

    /**
     * 每次插入3000个
     *
     * @param iService
     * @param dataList
     * @param <T>
     */
    public static <T> void loopBatchInsert(IService<T> iService, List<T> dataList) {
        if (dataList.size() > 3000) {
            loopBatchInsert(iService, dataList.subList(3000, dataList.size()));
            List sub = dataList.subList(0, 3000);
            iService.saveBatch(sub);
        } else {
            iService.saveBatch(dataList);
        }
    }

    /**
     * 获取list里重复元素
     *
     * @param list
     * @return
     */
    public static Set<String> same(List<String> list) {
        String[] indexArr;
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String key = list.get(i);
            String old = map.get(key);
            if (old != null) {
                map.put(key, old + "," + (i + 1));
            } else {
                map.put(key, "" + (i + 1));
            }
        }
        Iterator<String> it = map.keySet().iterator();
        int index = -1;
        Set<String> set = Sets.newHashSet();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            if (value.indexOf(",") != -1) {
                set.add(key);
                indexArr = value.split(",");

                for (int i = 0; i < indexArr.length; i++) {
                    index = Integer.parseInt(indexArr[i]) - 1;
                    list.set(index, list.get(index) + (1 + i));
                }
            }
        }
        return set;
    }

    public static HorizontalCellStyleStrategy getCellStyleStrategy() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体

        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 13);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
